package com.qqtop10.manager.controller;

import java.io.UnsupportedEncodingException;
import java.nio.charset.UnsupportedCharsetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtop10.manager.model.Admin;
import com.qqtop10.manager.model.Result;
import com.qqtop10.manager.model.SearchRecommendKeyword;
import com.qqtop10.manager.service.SearchRecommendKeywordService;

@Controller
@RequestMapping("/manager/recommend")
public class RecommendKeywordController extends BaseController {

	@Autowired
	@Qualifier("searchRecommend")
	private SearchRecommendKeywordService service;

	@RequestMapping("all.do")
	@ResponseBody
	public List<SearchRecommendKeyword> all() {
		return service.all();
	}

	// 添加推荐词
	@RequestMapping("addRecommend.do")
	@ResponseBody
	public Result addRecommend(HttpServletRequest request, HttpServletResponse response, SearchRecommendKeyword input)
			throws Exception {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		//String kw=getParameter(input.getKeyword());
		
		//input.setKeyword(kw);
		
		Admin admin = getLogind();
		input.setCreator(admin.getAccount());
		input.setMender(admin.getAccount());
		service.saveRecommend(input);
		return ok("操作成功");
	}

	// 设置推荐词显示或者隐藏
	@RequestMapping("showOrHiddenRecommend.do")
	@ResponseBody
	public Result showOrHiddenRecommend(HttpServletRequest request, HttpServletResponse response, String uuid,
			Integer isShow) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		Map<String, Object> map = new HashMap<>();
		if (isShow == null || uuid == null) {
			return err("参数错误");
		}
		if (isShow > 1 || isShow < 0) {
			return err("参数错误");
		}
		map.put("uuid", uuid);
		map.put("isShow", isShow);
		if (isShow == 1) {
			int count = service.getShowCount();
			if (count >= 7) {
				return err("推荐词最多只能显示7个");
			}
		}
		service.showOrHiddenRecommend(map);
		return ok("操作成功");
	}

	// 删除推荐词
	@RequestMapping("deleteRecommend.do")
	@ResponseBody
	public Result deleteRecommend(int ids) {
		int ret = service.deleteRecommend(ids);
		if (ret > 0) {
			return ok("操作成功");
		}
		return err("删除失败");
	}

	// 查询已显示的推荐词
	@RequestMapping("showRecommend.do")
	@ResponseBody
	public List<SearchRecommendKeyword> showRecommend(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		return service.showRecommend();
	}

	// 编辑推荐词
	@RequestMapping("updateRecommend.do")
	@ResponseBody
	public Result editRecommend(HttpServletRequest request, HttpServletResponse response, SearchRecommendKeyword input)
			throws Exception {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		Admin admin = getLogind();
		input.setMender(admin.getAccount());
		int ret = service.editRecommend(input);
		if (ret > 0) {
			return ok("操作成功");
		}
		return err("操作失败");
	}

}
